package file;

import java.io.File;
import java.io.FileFilter;

public class ListFilesDemo2 {
    public static void main(String[] args) {
        File dir = new File(".");
        if(dir.isDirectory()){
            FileFilter filter = new FileFilter() {
                //重写accept方法就是定义过滤器的接受条件
                //重写原则：当参数pathname对象是该过滤器接收的元素时反回true
                public boolean accept(File pathname) {
                    String name = pathname.getName();
                    return name.endsWith(".txt");
                }
            };
            File[] subs = dir.listFiles(filter);
            for (File sub :subs){
                System.out.println(sub.getName());
            }
        }
    }
}
